#include "processbar.h"

void processbar_version1()
{
    char buff[NUM];
    memset(buff, 0, sizeof(char) * NUM);

    const char *str = DRAW;
    int cur = 0;
    buff[0] = HEAD;
    while (cur <= 100)
    {
        printf("[%-100s][%3d%][%c]\r", buff, cur, str[cur % 4]);
        fflush(stdout);
        usleep(50000);

        buff[cur++] = FLAG;
        if (cur < 100)
            buff[cur] = HEAD;
    }

    printf("\n");
}

const char *str = DRAW;
static int count = 0;
char buff[NUM] = {0};

void Print(double rate)
{
  //  if (rate < 1.0)
  //      buff[0] = HEAD;

  //  printf("[%-100s][%.3f%%][%c]\r", buff, rate, str[count % 4]);
  //  fflush(stdout);
  //  
  //  if (rate > 1.0)
  //      buff[(int)rate - 1] = FLAG;
  //  if (rate == 100.0)
  //      buff[(int)rate] = FLAG;
  //  if ((int)rate < 99)
  //      buff[(int)(rate)] = HEAD;

       static int cnt = 0;
    int n = strlen(str);
    if(rate <= 1.0)
        buff[0] = HEAD;
    printf("[%-100s[%.1f%%][%c]\r", buff, rate, str[cnt%n]);
    fflush(stdout);

    buff[(int)rate] = FLAG;
    if((int)rate+1 < 100)
        buff[(int)(rate+1)] = HEAD;
    if(rate>=100.0)
        printf("\n");

    cnt++;
    cnt%=n;

}

void processbar_version2()
{
    srand((unsigned int)time(NULL));

    int total = FILESIZE;
    while (total > 0)
    {
        usleep(1000);
        int download = rand() % (4096 * 20) + 1;
        total -= download;
        if (total < 0)
            total = 0;

        double rate = (FILESIZE - total) * 1.0 / FILESIZE * 100;
     //   printf("%f\n", rate);
        Print(rate);
        count++;
    }

    printf("\n");
}

